import {Component, OnDestroy, OnInit} from '@angular/core';


@Component({
  selector: 'app-seckill-title',
  templateUrl: './seckill-title.component.html',
  styleUrls: ['./seckill-title.component.less']
})
export class SeckillTitleComponent implements OnInit, OnDestroy {
  countdown = {
    hour: '0',
    minute: '0',
    second: '0'
  };
  timedown;
  now = (new Date()).getTime();
  endTime = (new Date()).setHours(23, 59, 59);
  lastTime = Math.floor((this.endTime - this.now) / 1000);
  constructor() {
  }

  ngOnInit() {
    this.timeCountdown(this.lastTime);
  };
  timeCountdown (a: number) {
   this.timedown = setInterval(
      () => {
        let hour = '0';
        let minute = '0';
        let second = '0';
        if (a < 0) {
          a += 24 * 60 * 60 ;
        }
        if (a > 0) {
          hour = Math.floor(a / 3600) + '';
          minute = Math.floor((a - +hour * 3600) / 60 ) + '';
          second = Math.floor(a - +hour * 3600 - +minute * 60) + '';
        }
        if (+hour <= 9) {
          hour =  '0' + hour;
        }
        if (+minute <= 9) {
          minute =  '0' + minute ;
        }
        if (+second <= 9) {
          second =  '0' + second ;
        }
        this.countdown.hour = hour;
        this.countdown.minute = minute;
        this.countdown.second = second;
        a --;
      }, 1000
    );
  }
  ngOnDestroy() {
    clearInterval(this.timedown);
  }
}
